<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Facades\Auth;
use Closure;
use Illuminate\Support\Facades\Log;

class Authenticate
{
//    /**
//     * Get the path the user should be redirected to when they are not authenticated.
//     *
//     * @param  \Illuminate\Http\Request  $request
//     * @return string
//     */
//    protected function redirectTo($request)
//    {
//        if (! $request->expectsJson()) {
//            return redirect('relogin');
//        }
//        if (auth()->guard()->guest()){
//            return redirect('relogin');
//        }
//    }
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if(Auth::guard()->guest()){
            if ($request->ajax() || $request->wantsJson()){
                return redirect(env('APP_URL').route('relogin',null,false));
            }else{
                return redirect(env('APP_URL').route('relogin',null,false));
            }
        }
        return $next($request);
    }
}
